package com.pkk.components.util.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.BeanUtils;

/**
 * @description:
 * @author: peikunkun
 * @create: 2018-10-25 18:16
 **/
public class Beans {

  private Beans() {
  }

  public static <T> T copyTo(Object source, Class<T> targetClass, String... ignoreProperties) {
    Object t = null;
    try {
      t = targetClass.newInstance();
    } catch (Exception var5) {
      var5.printStackTrace();
    }
    BeanUtils.copyProperties(source, t, ignoreProperties);
    return (T) t;
  }

  public static <T> T copyTo(Object source, Class<T> targetClass) {
    return copyTo((Object) source, targetClass, (String[]) null);
  }

  public static <T> List<T> copyTo(Collection source, Class<T> targetClass) {
    return copyTo((Collection) source, targetClass, (String[]) null);
  }

  public static <T> List<T> copyTo(Collection source, Class<T> targetClass, String... ignoreProperties) {
    List<T> list = new ArrayList(source.size());
    source.iterator().forEachRemaining((o) -> {
      list.add(copyTo(o, targetClass, ignoreProperties));
    });
    return list;
  }
}
